package com.kyomonn.talkischeap.QuestionBank;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;

import com.kyomonn.talkischeap.CodingResources.CodingResourcesFragment;
import com.kyomonn.talkischeap.LoginActivity;
import com.kyomonn.talkischeap.MySQLiteOpenHelper;
import com.kyomonn.talkischeap.R;

public class QuestionBankFragment extends Fragment {

    private static QuestionBankViewModel viewModel;
    private SharedPreferences sharedPref;

    public static QuestionBankFragment Create() {
        return new QuestionBankFragment();
    }

    public static QuestionBankViewModel getViewModel() {
        if (viewModel == null) {
            viewModel = new QuestionBankViewModel();
        }
        return viewModel;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        sharedPref = requireContext().getSharedPreferences("user_data", Context.MODE_PRIVATE);

        if (!sharedPref.getBoolean("is_login", false)) {
            new AlertDialog.Builder(requireContext())
                    .setTitle("您还没有登录")
                    .setMessage("点击确定跳转到登录界面")
                    .setPositiveButton("确定", (dialog, which) -> startActivity(new Intent(requireContext(), LoginActivity.class)))
                    .setNegativeButton("取消", (dialog, which) -> {
                        CodingResourcesFragment codingResourcesFragment = new CodingResourcesFragment();
                        FragmentTransaction transaction = requireActivity().getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fragment_container_view, codingResourcesFragment);
                        transaction.commit();
                    })
                    .show();
            return null;
        }

        return inflater.inflate(R.layout.fragment_question_bank, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        QuestionBankViewModel viewModel = new ViewModelProvider(this).get(QuestionBankViewModel.class);

        QuestionBankAdapter adapter = new QuestionBankAdapter(requireContext(), R.layout.item_question_bank, viewModel.questionBankData);
        GridView gridView = view.findViewById(R.id.gv_question_bank);
        gridView.setAdapter(adapter);

        TextView username = view.findViewById(R.id.tv_username_question_bank);
        ImageView headshot = view.findViewById(R.id.iv_headshot_question_bank);
        TextView tvTopicsHaveBeenDone = view.findViewById(R.id.tv_topics_have_been_done);
        TextView tvPastPapersHaveBeenDone = view.findViewById(R.id.tv_past_papers_have_been_done);

        username.setText(sharedPref.getString("username", ""));

//        MySQLiteOpenHelper dbHelper = new MySQLiteOpenHelper(requireContext(), "user_data.db", null, 1);
//        SQLiteDatabase db = dbHelper.getWritableDatabase();
//        String sql = "SELECT COUNT(*) FROM questions WHERE user_answer IS NOT NULL;";
//        db.execSQL(sql, new String[]{});
//        dbHelper.close();
    }
}